window.workspace = function(){
	var response  = <?#$respons?>;
	var dform, dtree, ftree, win, layout;
	function addForm(node) {
		if(!win) {
		    dform = new Ext.form.FormPanel({
				frame		: true,
				border		: false,
				bodyStyle	: 'padding:8px;',
		        labelWidth	: 80,
		        labelAlign  : 'right',
		    	defaults	: {
		    		xtype	  : 'textfield'
		    	},
		    	items		: [{
					xtype		: 'hidden',
					name		: 'id',
					value		: 0
				},{
		    		xtype		: 'combo',
		            store		: new Ext.data.JsonStore({
			        	fields: ['id','name'],
			        	root	: 'dataset',
			        	autoLoad: true,
		            	proxy   : new Ext.data.HttpProxy({
							url : '/admin/district/doshare.html'
						})
			    	}),
					displayField	: 'name',
					valueField		: 'id',
					emptyText		: '请选择',
		            fieldLabel		: '顶级地域',
					minChars		: 1,
					 pageSize		: 10,
		            listWidth		: 258,
		            width			: 240,
					typeAhead		: true,
					typeAheadDelay  : 0,
					triggerAction	: 'all',
					enableKeyEvents : true,
					mode			: 'remote',
					hiddenName		: 'pid',
		            name			: 'pid'
				 },{
					xtype		: 'textfield',
					fieldLabel	: '地域名称',
					width		: 240,
					name		: 'name',
					allowBlank	: false
				},{
					xtype		: 'textfield',
					fieldLabel	: '排序信息',
					width		: 120,
					name		: 'rank',
					allowBlank	: false
				}]
			});
			win = new Ext.Window({
				title	: '新增资料',
				iconCls	: 'icon-add',	
				width	: 420,
				items	: [dform],
				closeAction : 'hide',
				bbar 	: {
					xtype		: 'toolbar',
					buttonAlign	: 'right',
					items		: [{
						text	: '保存', 
						iconCls	: 'icon-save',
						url		: '/admin/district/dosave.html',
						handler	: function(){
						if(!dform.getForm().isValid())
		        			return;
		        		dform.getForm().submit({
			        		waitMsg 	: '数据提交处理中...',
							waitTitle 	: '信息提示',
			        		clientValidation: true,
			        		url		: '/admin/district/dosave.html',
				        	success	: function(form, action) {
				        		dtree.getRootNode().reload();
				        		win.hide();
							},
							failure	: main.submitFailure
				    	});
					}}]
				}
			});
			window.onresize = function () {
				win.center();
			}
		}
		var id = Ext.isObject(node)? node.attributes.id:0;
		win.setTitle((id > 0)? '编辑资料':'新增资料');
		win.setIconClass((id > 0)? 'icon-edit':'icon-add');
		win.show();
		dform.getForm().reset();
		if(id > 0) {
			var obj = null;
			for(var i in node.attributes) {
				if(Ext.isString(i) && (obj = dform.getForm().findField(i))) {
					obj.setValue(node.attributes[i]);
				}
			}
			var text = '';
			node     = node.parentNode;
			while(node && !!node.attributes.name) {
				text = node.attributes.name + text;
				node = node.parentNode;
			}
			dform.getForm().findField('pid').setRawValue((!!text)? text:'顶级地域');
		}
	}
	dtree = new Ext.ux.tree.TreeGrid({
		tbar			: [{
			text	: '刷新',
			iconCls	: 'icon-refresh',
			handler : function(){
				dtree.getRootNode().reload();
			}
		},{
			text	: '新增资料',
			iconCls	: 'icon-add',
			url		: '/admin/district/dosave.html',
			handler : function(){
				addForm(0);
			}
		},'-',{
			text	: '删除资料',
			iconCls	: 'icon-delete',
			url		: '/admin/district/doremove.html',
			handler : function() {
				var sm = dtree.getSelectionModel(); 
				var rs = sm.getSelectedNode();
				if(!rs) {
					Ext.Msg.alert('信息提示', '请选择要删除的记录！'); 
					return;
				} else if(!rs.attributes.leaf) {
					Ext.Msg.alert('信息提示', '请先删除子节点记录！'); 
					return;
				}
				var pnode = rs.parentNode;
				Ext.Msg.confirm('信息提示', '您确认要删除选中的记录？删除将不能恢复。',
				function(btn){
					if(btn != 'yes') {
						return;
					}
			        Ext.Ajax.request({
			        	url 	: '/admin/district/doremove.html',
			        	params	: {ids:rs.attributes.id},
			        	success	: function(resp, req) {
			        		var data = Ext.decode(resp.responseText);
			        		if(data.code != 0) {
			        			Ext.Msg.alert('信息提示', data.msg); return;
			        		}
			        		(pnode && pnode.attributes.id > 0)? pnode.reload():dtree.getRootNode().reload();
			        	}
			        });
				});
			}
		}],
		region		: 'center',
        columns:[{
            header: '地域名称',
            dataIndex: 'name',
            width: 260
        },{
            header: '流水号',
            dataIndex: 'id',
            width: 100
        },{
            header: '排序',
            width: 100,
            dataIndex: 'rank'
        }],
        loader 			: {
			dataUrl		: '/admin/district/dotree.html'
		},
		listeners 		: {
			'dblclick' : function(node){
				addForm(node);
			}
		}
    });
	layout = {
		title	: response.title,
		layout	: 'border',
		region	: 'center', 
		items	: [dtree],
		listeners : {'destroy':function(){
	    	win && win.close();
	    }}
	}
	return layout;
}();